/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.src.uniandes.ecos.tsp.creditscore.algorithm;

import com.src.uniandes.ecos.tsp.creditscore.entity.ClienteProducto;
import com.src.uniandes.ecos.tsp.creditscore.entity.TipoArticulo;
import com.src.uniandes.ecos.tsp.creditscore.exception.CreditScoreException;
import com.src.uniandes.ecos.tsp.creditscore.jpa.ClienteProductoJpaController;
import java.util.Vector;

/**
 * This class allow to know the number of points that a user have
 * according to the type of credit. This class implements a rules
 * to know the points.
 *
 * @version 1.1 01 Apr 2010
 * @author Rodrigo Garcia
 */
class RulesTypeCredit {

   /**
     * This constant represents the product type: credit card.
     */
    private static final Integer CREDIT_CARD = 1;

     /**
     * This constant is used if the user bought car
     */
    private static final Integer POINTS_CAR = 10;

     /**
     * This constant is used if the user bought house
     */
    private static final Integer POINTS_HOUSE = 10;


     /**
     * This constant is used if the user bought study
     */
    private static final Integer POINTS_STUDY =8;

     /**
     *  This constant is used if the user bought motorcycle
     */
    private static final Integer POINTS_MOTORCYCLE =8;

     /**
     * This constant is used if the user bought viveres
     */
    private static final Integer POINTS_VIVERES = 4;


     /**
     *  This constant is used if the user bought electrical
     */
    private static final Integer POINTS_ELECTRICAL = 6;

    /**
     *  This constant is used if the user minor items
     */
    private static final Integer POINTS_MINOR_ITEMS = 2;



    /**
     * Returns the points number that client obtains according with its types credit.
     * @param codigoCliente the client.
     * @return the points number that client obtains according with its types credit.
     */
    double  getPointCreditType(Integer codigoCliente) throws CreditScoreException  {
        double points = 0.0;
        try {
        
            ClienteProductoJpaController cp = new ClienteProductoJpaController();
            Vector<ClienteProducto>  productos = (Vector<ClienteProducto>) cp.findClienteProductoEntities(codigoCliente);
             for (ClienteProducto producto : productos) {
                 TipoArticulo ta = producto.getArtCodigo();

                 if ( !producto.getProducto().getTipCodigo().getTipCodigo().equals(CREDIT_CARD) ) {

                     // bought car
                     if (  ta.getArtCodigo().equals(1) ){
                         points = points + POINTS_VIVERES;
                     }
                     // bought house
                     if (  ta.getArtCodigo().equals(2) ){
                         points = points + POINTS_CAR;
                     }
                     // bought mototcycle
                     if (  ta.getArtCodigo().equals(3) ){
                         points = points + POINTS_MOTORCYCLE;
                     }

                     // bought study
                     if (  ta.getArtCodigo().equals(4) ){
                         points = points + POINTS_HOUSE;
                     }

                     // bought electrical
                     if (  ta.getArtCodigo().equals(5) ){
                          points = points + POINTS_STUDY ;
                     }

                      // bought viveres
                      if (  ta.getArtCodigo().equals(6) ){
                          points = points + POINTS_ELECTRICAL;
                     }

                       // bought viveres
                      if (  ta.getArtCodigo().equals(7) ){
                          points = points + POINTS_MINOR_ITEMS;
                     }

                     } else {
                          // bought minor items
                           points = points + POINTS_MINOR_ITEMS;
                     }
             }


             points = points / productos.size();

             return points;

        } catch (Exception e) {
            throw new CreditScoreException("RulesTypeCredit:getPointCreditType:" + e.toString(), e);
        }

    }

}